// MessageControl.cpp 修改后
#include "messagecontrol.h"
#include "ui_messagecontrol.h"

MessageControl::MessageControl(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::MessageControl)
{
    ui->setupUi(this);

    // 隐藏默认标题栏
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    // setAttribute(Qt::WA_TranslucentBackground);


    connect(ui->btn_close, &QPushButton::clicked, this, &MessageControl::close);
}


MessageControl::~MessageControl()
{
    delete ui;
}

void MessageControl::on_pushButton_clicked()  // 清空消息
{
    emit clearMessage();
    close();
}

void MessageControl::on_pushButton_2_clicked()  // 导出消息
{
    emit exportMessage();
    close();
}

void MessageControl::on_pushButton_3_clicked()  // 清空报警
{
    emit clearAlarm();
    close();
}

void MessageControl::on_pushButton_4_clicked()  // 导出报警
{
    emit exportAlarm();
    close();
}

void MessageControl::updateMessageFilter()
{
    QStringList selected;
    if (ui->checkBox->isChecked())     selected << "Alarm";
    if (ui->checkBox_2->isChecked())   selected << "Warning";
    if (ui->checkBox_3->isChecked())   selected << "Error";

    emit messageFiltersChanged(selected);
}

void MessageControl::on_checkBox_stateChanged(int) {
    updateMessageFilter();
}

void MessageControl::on_checkBox_2_stateChanged(int) {
    updateMessageFilter();
}

void MessageControl::on_checkBox_3_stateChanged(int) {
    updateMessageFilter();
}

void MessageControl::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        QPoint posInTitleBar = ui->titleBarWidget->mapFrom(this, event->pos());
        if (ui->titleBarWidget->rect().contains(posInTitleBar)) {
            m_mousePressed = true;
            m_dragPos = event->globalPosition().toPoint() - frameGeometry().topLeft();
        }
    }
}

void MessageControl::mouseMoveEvent(QMouseEvent *event)
{
    if (m_mousePressed && (event->buttons() & Qt::LeftButton)) {
        move(event->globalPosition().toPoint() - m_dragPos);
    }
}

void MessageControl::mouseReleaseEvent(QMouseEvent *)
{
    m_mousePressed = false;
}

